package com.yw.swagger.controller;

import com.yw.swagger.customNote.qw;
import com.yw.swagger.model.AppAuthor;
import com.yw.swagger.service.AuthorService;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.Utils.AuthorUtils;
import com.yw.swagger.tool.Utils.StringUtils;
import com.yw.swagger.tool.controller.BaseController;
import com.yw.swagger.tool.page.TableDataInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: Abeiv
 * @Description:
 * @Date: 2019/10/26 11:06
 */
@Api(tags = "权限码接口")
@Controller
@RequestMapping("/system")
public class AuthorController extends BaseController {

    @Autowired
    private AuthorService authorService;


    /**
     * 权限码生成
     * @return
     */
    @qw
    @ApiOperation(value = "权限码生成",notes = "权限码生成")
    @GetMapping(value = "/getAuthor")
    @ResponseBody
    public AjaxResult getAuthor(){
        String str = AuthorUtils.getAuthor(10);

        logger.info(str);
        return AjaxResult.success(str);
    }

    @qw
    @ApiOperation(value = "新增权限码",notes = "新增权限码")
    @ApiImplicitParam(name="author",value="权限码",required = true)
    @PostMapping("/add_author")
    @ResponseBody
    public AjaxResult addAuthor(AppAuthor author){
        if(StringUtils.isNull(author)){
            return error("参数无效");
        }
        if(StringUtils.isNull(author.getAuthorNum())){
            author.setAuthorNum(AuthorUtils.getAuthor(10));
        }
        return AjaxResult.success(authorService.addAuthor(author));
    }

    @qw
    @ApiOperation(value = "删除权限码",notes = "删除权限码")
    @ApiImplicitParam(name="author",value="权限码",required = true)
    @PostMapping("/del_author")
    @ResponseBody
    public AjaxResult delAuthor(Long authorId){
        if(StringUtils.isNull(authorId)){
            return error("参数无效");
        }
        AppAuthor author = new AppAuthor();
        author.setId(authorId);
        getSession().removeAttribute("sign");
        getSession().setMaxInactiveInterval(60*60*24);
        getSession().setAttribute("sign",authorService.selectAuthorList());
        return AjaxResult.success(authorService.delAuthor(author));
    }

    @qw
    @ApiOperation(value = "分页查询权限码",notes = "分页查询权限码")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="数据条数",required = true)
    })
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult toList(Integer pageNum, Integer pageSize) {
        startPage();
        List<AppAuthor> l = authorService.selectAuthorList();
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @qw
    @ApiOperation(value = "权限码详情",notes = "权限码详情")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="code",value="要查询的权限码",required = true)
    })
    @PostMapping("/author_info_code")
    @ResponseBody
    public AjaxResult findoneBycode(String code) {
        if(StringUtils.isNull(code)){
            return error("参数无效");
        }
        return AjaxResult.success(authorService.selectAuthorByCode(code));
    }

    @qw
    @ApiOperation(value = "权限码详情",notes = "权限码详情")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="id",value="要查询的id",required = true)
    })
    @PostMapping("/author_info_id")
    @ResponseBody
    public AjaxResult findoneById(String id) {
        if(StringUtils.isNull(id)){
            return error("参数无效");
        }
        return AjaxResult.success(authorService.selectAuthorByCode(id));
    }

}
